package com.saver.view.item;

import android.app.FragmentManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.saver.R;
import com.saver.core.persistence.DBManager;
import com.saver.core.util.CommonUtil;
import com.saver.core.processor.util.Util;
import com.saver.core.util.Constantes;
import com.saver.view.TransaccionView;
import com.saver.view.adapter.TransactionListAdapter;
import com.saver.view.dialog.EditTransactionDialog;
import com.saver.view.dialog.RefreshActivity;
import com.saver.view.gui.FragmentSupport;
import com.saver.view.model.FragmentModel;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListRegistroFragment extends FragmentSupport implements OnItemClickListener, RefreshActivity {

	public static final String TAG = "ListRegistroFragment";
	
    public static final String KEY_CATEGORY_NAME = "category_name";
    public static final String KEY_INSTITUTION = "institution";
    public static final String KEY_EVENT_DATE = "event_date";
    public static final String KEY_MONEY_IMPORT = "money_import";
    public static final String KEY_ICON_CATEGORY = "icon_category";
    public static final String KEY_COLOR = "color";
    public static final String KEY_TRANSACTION = "transaction_id";

    private ListView listView;
    
    List<TransaccionView> listaTransaccion = new ArrayList<TransaccionView>();

    Integer anio;
    Integer mes;
    Integer dia;

    public static ListRegistroFragment instanceOf(Bundle args) {
        ListRegistroFragment fragment = new ListRegistroFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_list_main, container, false);
        listView = (ListView) view.findViewById(R.id.listViewDetail);
        listView.setOnItemClickListener(this);
        inicio();
        return view;
    }

	private void inicio() {
		createTransaction();
        DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
        ArrayList<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
        String string;
        String eventDate;
        for (TransaccionView transaccion : listaTransaccion) {
            eventDate = df.format(transaccion.getFechaTransaccion());
            Map<String, String> data = new HashMap<String, String>();
            data.put(KEY_CATEGORY_NAME, (string = transaccion.getNombreCategoria()) != null ? string : "");
            String detalle = transaccion.getReferencia();
            if (Util.esVacio(detalle)) {
                detalle = transaccion.getNombreEstablecimiento();
                if(Util.esVacio(detalle)){
                	detalle = transaccion.getNombreCategoriaEquiv();
                }
            }
            data.put(KEY_INSTITUTION, detalle);
            data.put(KEY_EVENT_DATE, eventDate);
            data.put(KEY_MONEY_IMPORT, CommonUtil.formatCurrency(transaccion.getMonto()));
            data.put(KEY_ICON_CATEGORY, transaccion.getIcono());
            data.put(KEY_COLOR, transaccion.getColor());
            data.put(KEY_TRANSACTION, String.valueOf(transaccion.getId()));
            dataList.add(data);
        }
        TransactionListAdapter adapter = new TransactionListAdapter(getActivity(), dataList);
        listView.setAdapter(adapter);
	}

    private void createTransaction() {
        FragmentModel model = (FragmentModel) getArguments().getSerializable("args");
        int tipoConsulta = model.getTipoConsulta();
        Log.d(TAG, "tipoConsulta: " + tipoConsulta);
        
        Calendar calendar = model.getCalendar();
        Log.d(TAG, "calendar: " + calendar);
        
        anio = calendar.get(Calendar.YEAR);
        mes = calendar.get(Calendar.MONTH) + 1;
        dia = calendar.get(Calendar.DATE);

        if (tipoConsulta == Constantes.CONSULTA_X_ANIO) {
            listaTransaccion = DBManager.transaccionViewSQLite().listarTransaccionesXCategoria(model.getCategoria(), anio);
        } else if (tipoConsulta == Constantes.CONSULTA_X_MES) {
            listaTransaccion = DBManager.transaccionViewSQLite().listarTransaccionesXCategoria(model.getCategoria(), anio, mes);
        } else if (tipoConsulta == Constantes.CONSULTA_X_DIA) {
            listaTransaccion = DBManager.transaccionViewSQLite().listarTransaccionesXCategoria(model.getCategoria(), anio, mes, dia);
        } else if (tipoConsulta == Constantes.CONSULTA_X_REVISAR) {
            listaTransaccion = DBManager.transaccionViewSQLite().listarTransaccionesXRevisar();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Integer transactionID = Integer.valueOf((String) view.getTag());
        FragmentManager manager = getActivity().getFragmentManager();
        EditTransactionDialog dialog = new EditTransactionDialog();
        dialog.setParent(this);
        dialog.setTransaccionID(transactionID);
        dialog.show(manager, "Editar Transaccion");
    }

    @Override
    public void refresh(Object params) {
        inicio();
    }

    @Override
    public void reemplazarFragmento(FragmentSupport fragment) {

    }

    @Override
    public void selectedIndex(int position) {

    }
}
